package com.hyg.module_user.systemSetting;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.hyg.lib_base.Net.NetworkUtils;
import com.hyg.lib_base.mainUtils.StatusBarUtil;
import com.hyg.lib_base.mainUtils.Utils;
import com.hyg.lib_common.Base.BaseActivity;
import com.hyg.lib_common.constant.UrlUtils;
import com.hyg.lib_common.constant.Constants;
import com.hyg.lib_common.sweetalertdialog.SweetAlertDialog;
import com.hyg.module_user.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.lang.ref.WeakReference;
import java.net.URLDecoder;

import okhttp3.FormBody;

@Route(path = Constants.PATH_ACTIVITY_MODULE_USER_SETTING_SUGGEST)
public class SuggestFeedbackActivity extends BaseActivity {

    protected void onStop(){
        super.onStop();
        if(this.isFinishing()){
            myHandler.removeCallbacksAndMessages(null);
        }
    }

    private ImageView back;
    private TextView tv_phone;
    private EditText et_suggest;
    private Button submit_suggest;


    private MyHandler myHandler = new MyHandler(this);
    private static class MyHandler extends Handler {
        private final WeakReference<Context> reference;
        private SuggestFeedbackActivity activity;
        public MyHandler(Context context) {
            reference = new WeakReference<>(context);
            activity = (SuggestFeedbackActivity) reference.get();
        }
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
                switch (msg.what) {
                    case 1://提交建议
                        try {
                            JSONObject jsondata = new JSONObject(msg.obj.toString());//获取返回的JSONObject原对象
                            String status = URLDecoder.decode(jsondata.getString("code"), "utf-8");
                            String message = URLDecoder.decode(jsondata.getString("message"), "utf-8");
//                        Log.d("welcomeadLogin", jsondata + "");
                            if (status.equals("200")) {
                                if (activity.pDialog.isShowing())
                                    activity.pDialog.dismiss();
                                activity.pDialog = new SweetAlertDialog(activity, SweetAlertDialog.SUCCESS_TYPE);
                                activity.pDialog.setTitleText("提交成功");
                                activity.pDialog.setCancelable(false);
                                activity.pDialog.setContentText("感谢您的宝贵意见，我们将会继续改进!");
                                activity.pDialog.setConfirmButton("好的", new SweetAlertDialog.OnSweetClickListener() {
                                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                                        sweetAlertDialog.cancel();
                                        activity.finish();
                                        activity.overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
                                    }
                                });
                                activity.pDialog.show();
                            } else {
                                activity.ErrorDialog(message);
                            }
                        } catch (JSONException e) {
                            Log.d(CrashTAG,""+e.getMessage());
                        } catch (UnsupportedEncodingException e) {
                            Log.d(CrashTAG,""+e.getMessage());
                        }
                        break;
                }
            }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_suggest_feedback);
        StatusBarUtil.setStatusBar(1,this,getResources().getColor(R.color.white),00,null,null,true);
        init();
    }
    public void init(){
        tv_phone = (TextView)findViewById(R.id.tv_phone);
//        tv_phone.setText("联系电话："+sn.getString("phone","").substring(0,3)+"****"+sn.getString("phone","").substring(7));
        et_suggest = (EditText) findViewById(R.id.et_suggest);

        submit_suggest = (Button) findViewById(R.id.submit_suggest);
        submit_suggest.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {if(Utils.isFastClick()){return ;}
                if(et_suggest.getText().toString().trim().equals("")){
                    if (pDialog.isShowing())
                        pDialog.dismiss();
                    pDialog = new SweetAlertDialog(SuggestFeedbackActivity.this, SweetAlertDialog.WARNING_TYPE);
                    pDialog.setTitleText("当前内容为空，不可提交");
                    pDialog.setConfirmButton("知道啦", new SweetAlertDialog.OnSweetClickListener() {
                        public void onClick(SweetAlertDialog sweetAlertDialog) {
                            sweetAlertDialog.cancel();
                        }
                    });
                    pDialog.show();
                }else{
                    getHealthData(et_suggest.getText().toString().trim());
                }
            }
        });

        back = (ImageView)findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {if(Utils.isFastClick()){return ;}
                SuggestFeedbackActivity.this.finish();
                overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
            }
        });
    }
    public void getHealthData(String advice){
        if(UINetWork(NetworkUtils.isConnected(this)))//检查网络是否断开的 提示，决定是否访问服务器
        {
            String str_tag = "1";
            FormBody formBody = new FormBody.Builder()
                    .add("advice", advice)//设置参数名称和参数值
                    .add("token", sn.getString("token", ""))//设置参数名称和参数值
                    .build();
            sendPostNetwork(str_tag, formBody, myHandler, UrlUtils.feedbackAddr);
        }
    }

}